<?php
/**
 *******************************************************************
 ************* ─────────────────── *********************************
 ************* ── 代发货平台系统官方正式版 ── **********************
 ************* ─────────────────── *********************************
 ************* 官方网站：http://order.ai-pay.com/   ****************
 ************* 官方店铺：http://web101.taobao.com/  ****************
 ************* 程序开发：淘宝小赢家                 ****************
 ************* 客户服务：[淘宝旺旺] 淘宝小赢家 *********************
 ************* 技术支持：[QQ] 1752174348            ****************
 *******************************************************************
 *************   官方正版 *** 版权所有 *** 盗版必究   **************
 *******************************************************************
 */
class users extends spController{
	public function __construct() {
		//登录验证	
		if(!isset($_SESSION['IS_LOGIN']) || !$_SESSION['IS_LOGIN']){
			$this->jump (spUrl ('login','index'));
		}
		parent::__construct();
		$this -> _controller = 'users';
		$this -> _pk = spClass('MUser') -> pk;
		$this -> _attributes = spClass('MUser') -> getAttributes();
		$this -> _attributeFormset = spClass('MUser') -> getAttributeFormsets();
		$this -> savesession = $_SESSION['IS_LOGIN'];
		$this -> savesessionid = $_SESSION['LOGIN_ID'];
		$this->spAcl = spClass("spAcl")->get();
		spAddViewFunction('cbForm',array('LibCodeBuilder','cbFormWidget'));
	}
	
	public function index(){
		$this -> _attributes = spClass('MUser') -> getUnFilteAttributes();
		$this -> tpl_page = $page = $this -> spArgs('page',1);
		$this -> tpl_datas = spClass('MUser') -> spPager($page, 30) -> findAll();
		$this -> tpl_pager = spClass('MUser') -> spPager() -> getPager();
		$this -> display('users/search.html');
	}
	
	public function view(){
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');
		$this -> tpl_data = spClass('MUser') -> find($condition);
		$this -> display('users/view.html');
	}
	
	public function create(){
		if($_POST){
			$rows = spClass('spArgs')->get($this->_controller, null, 'post');
			$rows['pwd'] = md5($rows['pwd']);
			
			if(($id = spClass('MUser')->create($rows)))
				$this ->success ('新增数据成功',  spUrl ($this->_controller, 'view', array('id'=>$id)));
			else
				$this ->error ('新增数据失败', 'javascript:history.go(-1)');
		}
		$this ->display('users/create.html');
	}
	
	public function update(){
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');
		$this->updateData($condition);
		$this ->display('users/update.html');
	}	
	
	public function updateme(){
		$condition = array();
		$condition['id'] = $_SESSION['LOGIN_ID'];
		$this->updateData($condition);
		$this ->display('users/updateme.html');
	}
	
	public function updateData($condition){
		if($_POST){
			$rows = spClass('spArgs')->get($this->_controller, null, 'post');
	
			if($this->spAcl != "ADMIN"){
				if($rows['isflag'] != md5($rows['pwd']) || $rows['newpwd'] == ''){
					$this ->error ('修改失败，旧密码不对 或 新密码不能为空！', 'javascript:history.go(-1)');
					exit;
				}
				
				$rows['pwd'] = $rows['newpwd'];
				unset($rows['newpwd']);
				unset($rows['isflag']);
			}
			
			if($rows['pwd']){
				$rows['pwd'] = md5($rows['pwd']);
			}else{
				unset($rows['pwd']);
			}
			
			if(spClass('MUser')->update($condition,$rows)){
                if($this->spAcl != "ADMIN"){
                    $this ->success ('修改成功',  spUrl ($this->_controller, 'updateme'));
                }else{
                    $this ->success ('修改成功',  spUrl ($this->_controller, 'search'));
                }
            }
			else
				$this ->error ('修改失败', 'javascript:history.go(-1)');
		}
		
		
		if($this->spAcl == "ADMIN"){
			$this -> _attributes = spClass('MUser') -> getAttributes();
		}else{
			$this -> _attributes = array(
				'pwd'		=>	'旧密码',
				'newpwd'	=>	'新密码',
			);
		}
		
		$this -> tpl_data = spClass('MUser') -> find($condition);		
	}
	
	public function delete(){
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');
		$success = spClass('MUser') -> delete($condition);
		if($success)
			$this ->success('删除成功', $this->spArgs('redirect','javascript:history.go(-1)'));
		else
			$this ->error('删除失败', $this->spArgs('redirect','javascript:history.go(-1)'));
	}
	
	public function search(){
		$this -> _attributes = spClass('MUser') -> getUnFilteAttributes();
		if($_POST){
			$this -> _attributes = spClass('MUser') -> getUnFilteAttributes();
			$this -> tpl_keyword = $conditions = spClass('spArgs')->get($this->_controller, null, 'post');
			$this -> tpl_searchType = $searchType = spClass('spArgs')->get($this->_controller.'_searchType', null, 'post');
			$where = array();
			foreach($conditions as $key => $value){
				if(empty($value)) continue;
				if(empty($this->_attributes[$key])) continue;
				if($searchType[$key] == 1) $where[] = ' '.$key.' LIKE '.spClass('MUser')->escape($value).' ';
				else $where[] = ' '.$key.' LIKE '.spClass('MUser')->escape('%'.$value.'%').' ';
			}
			$this -> tpl_page = $page = $this -> spArgs('page',1);
			$this -> tpl_datas = spClass('MUser') -> spPager($page, 30) -> findAll(implode('AND',$where));
			$this -> tpl_pager = spClass('MUser') -> spPager() -> getPager();
		}else{		
			$this -> tpl_page = $page = $this -> spArgs('page',1);
			$this -> tpl_datas = spClass('MUser') -> spPager($page, 30) -> findAll();
			$this -> tpl_pager = spClass('MUser') -> spPager() -> getPager();
		}
		$this -> display('users/search.html');
	}
}